//
//  RadioViewController.m
//  Slice
//
//  Created by lanou on 16/6/15.
//  Copyright © 2016年 wangjinchao. All rights reserved.
//

#import "RadioViewController.h"
#import "RadioListTableViewController.h"
@interface RadioViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation RadioViewController

-(NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return  _dataArray;
}

-(void)getData {
    [RequestTool requsetWithType:GET URLString:kRatioURL paremeter:nil callBack:^(NSData *data, NSError *error) {
        if (data.length != 0) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
            
            NSArray *arr = jsonDic[@"data"][@"alllist"];
            NSMutableArray *marr = [NSMutableArray array];
    
            for (NSDictionary *dic1 in jsonDic[@"data"][@"carousel"]) {
                [marr addObject:dic1[@"img"]];
            }
            
            for (NSDictionary *dic in arr) {
                RadioTypeModel *model = [RadioTypeModel modelWithDiction:dic];
                [self.dataArray addObject:model];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                if (self.tableView.tableFooterView == nil) {
                    //            初始化轮播图
                    CoueousPic *round = [[CoueousPic alloc] initWithFrame:CGRectMake(0, 64, kWidth, kHeight - kWidth - 5 - 64) images:marr];
                    self.tableView.tableHeaderView = round;
                }
                
                [self.tableView reloadData];
                [self.tableView.mj_header endRefreshing];
            });
        }
    }];
}

- (void)loadMoreData {
    [RequestTool requsetWithType:POST URLString:kRatioMoreURl paremeter:@{@"start":@(self.dataArray.count)} callBack:^(NSData *data, NSError *error) {
        if (data == nil) {
            return ;
        }
        NSDictionary *moreData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *dic in moreData[@"data"][@"list"]) {
          [self.dataArray addObject: [RadioTypeModel modelWithDiction:dic]];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            [self.tableView.mj_footer endRefreshing];
        });
    }];
}


- (void)creatTableView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
//    添加下拉加载
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadMoreData];
    }];
//    添加下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataArray removeAllObjects];
        [self getData];
    }];
}





- (void)viewDidLoad {
    [super viewDidLoad];
     self.view.backgroundColor = [UIColor whiteColor];
    
    [self getData];
    
    [self creatTableView];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    self.navigationItem.title = @"电台";
}



#pragma mark -----代理方法-------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *str = @"reuse";
    RadioTypeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[RadioTypeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
        
    }
    [cell getDataByModel:self.dataArray[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    RadioListTableViewController *vc = [[RadioListTableViewController alloc] init];
    RadioTypeModel *model = self.dataArray[indexPath.row];
    vc.radioID = model.radioid;
    vc.name = model.title;
    [self.navigationController pushViewController:vc animated:YES];
}




@end
